<?php
 
 //GET CRUMB
 	
	define ('HOSTNAME', 'http://web101.a1.rest.chat.sp1.yahoo.com/v1/'); 
	$url = HOSTNAME.'session' ;
	$cookies = "Y=".$_COOKIE["Y"]."; T=".$_COOKIE["T"]."; B=".$_COOKIE["B"];
	 
	 
	// Open the Curl session
	$session = curl_init($url);
	   
	// Don't return HTTP headers. Do return the contents of the call
	curl_setopt($session, CURLOPT_HEADER, false);
	curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($session, CURLOPT_COOKIE, $cookies); 
	
	// Make the call
	$xml  = simplexml_load_string( curl_exec($session) );
	
	// The web service returns XML. Set the Content-Type appropriately
	//header("Content-Type: text/xml");
	
	$crumb = $xml->crumb;
	echo("CRUMB " . $xml);
	curl_close($session);
 
 
 //LOGIN to CHAT SPACE
 	$url = HOSTNAME.'session;crumb='.$crumb ;
 	$post_string = " <request><role>1</role></request>";  
	$ch = curl_init($url); 
	  
	curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
	curl_setopt($ch, CURLOPT_COOKIE, $cookies);
	curl_setopt ($ch, CURLOPT_POST, true); 
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

	$xml  = simplexml_load_string( curl_exec($ch) );
	$chatid = $xml->chatId;
	//echo($chatid);
	curl_close($ch);
	
 
//JOIN A CHAT SPACE
 	$url = HOSTNAME.'space;crumb='.$crumb.';appId=appid-002' ;
 	$post_string = "<req><role>1</role><captchaFlag>1</captchaFlag><roomId>CONF_appid-002_91b083f43ab14fe4b8c84d286d021f3a</roomId>";
 	$post_string .= "<chatId>"  .$chatid   ."</chatId></req>"; 
 	//echo($post_string);
	
	$ch = curl_init($url); 
	  
	curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
	curl_setopt($ch, CURLOPT_COOKIE, $cookies);
	curl_setopt ($ch, CURLOPT_POST, true); 
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

	$data = curl_exec($ch); 
	echo($data);
	curl_close($ch);
	 
  
?>